import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Injectable, Inject } from '@nestjs/common';
import { Cache } from 'cache-manager';

@Injectable()
export class RedisCacheService {
  constructor(
    @Inject(CACHE_MANAGER) private readonly cache: Cache,
  ) {}

  async get(key) {
    return await this.cache.get(key);
  }

  async set(key, value, ttl?: number) {
    return await this.cache.set(key, value, ttl || 1000 * 60 * 60 * 24 * 15);
  }

  async del(key){
    return await this.cache.del(key)
  }

  async clear(key){
    let cacheKeys = await this.cache.store.keys(key)
    await Promise.all(cacheKeys.map(k => this.cache.del(k)));
  }
}
